package models

import (
	"fmt"
	"good/conf"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

type Good struct {
	Id        int     `gorm:"type:int auto_increment" json:"id"`
	Name      string  `gorm:"type:varchar(30)" json:"name" valid:"Required"`
	Price     float64 `gorm:"type:decimal(10,2)"json:"price" valid:"Required"`
	Weight    int     `gorm:"type:int" json:"weight" valid:"Required"`
	Num       int     `gorm:"type:int" json:"num" valid:"Required"`
	Times     string  `gorm:"type:varchar(50)" json:"times" valid:"Required"`
	DeletedAt gorm.DeletedAt
}

var (
	DB  *gorm.DB
	err error
)

func MysqlInit() {
	var Conf = conf.Con.MysqlConfig
	fmt.Println(Conf)
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", Conf.User, Conf.Password, Conf.Host, Conf.Port, Conf.Database)
	DB, err = gorm.Open(mysql.Open(dsn))
	if err != nil {
		fmt.Println("连接数据库失败")
		return
	}
	fmt.Println("连接数据库成功")
	DB.AutoMigrate(&Good{})
}

type JsonR struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}
